home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.IO;
- using System.Net;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Xml;
- using GBPVR.Public;
-
- namespace gbweb
- {
- /// <summary>
- /// Summary description for Login.
- /// </summary>
- public partial class Login : Page
- {
- protected string salt;
- private string requiredGuestPasswordHash;
- private String requiredUsername;
- private String requiredPasswordHash;
- private string hostAddress = string.Empty;
- protected Settings guideParams;
-
- protected void Page_Load(object sender, EventArgs e)
- {
- getTheme();
-
- Session["GuideStartTime"] = null;
- Session["NotGuestUser"] = "true";
- logo.Src = logo.Src.Replace("images", Convert.ToString(Session["theme"]));
-
- Logger.Info("Loading Config...");
- XmlDocument configDoc = Global.Config;
-
- // extract username and password
- requiredUsername = "admin";
- requiredPasswordHash = string.Empty;
- XmlNode node = configDoc.SelectSingleNode("/settings/WebUsername");
- if (node != null)
- {
- requiredUsername = node.InnerText;
- }
- node = configDoc.SelectSingleNode("/settings/WebPassword");
- if (node != null)
- {
- requiredPasswordHash = node.InnerText;
- }
- guideParams = Global.Settings;
- requiredGuestPasswordHash = guideParams.GuestPassword;
- Logger.Info(" ");
- Logger.Info("Pulled Credential from Config...");
- Logger.Info("User= " + requiredUsername);
- Logger.Info("Password = " + requiredPasswordHash);
- Logger.Info(" ");
- if (!IsPostBack)
- {
- Logger.Info("Postback not found....now pulling User Host Info...");
- if (Request.UserHostAddress.Substring(0, 1) == ":")
- {
- Logger.Info("IP V6 Detected on Server ");
- if (Global.Settings.autologinNets.CheckNumber(GetIP4Address()))
- {
- hostAddress = GetIP4Address();
- Logger.Info("Using IP4 Address: " + hostAddress);
- Logger.Info(" ");
- CompleteLogin();
- }
- }
- else
- {
- Logger.Info("IP V4 Detected on Server");
- if (Global.Settings.autologinNets.CheckNumber(Request.UserHostAddress))
- {
- hostAddress = Request.UserHostAddress;
- Logger.Info("Using IP4 Address: " + hostAddress);
- Logger.Info(" ");
- CompleteLogin();
- }
- }
- }
- Logger.Info(" ");
- Logger.Info("Setting logo attribute to version info...");
- logo.Attributes.Add("title", Global.Settings.GetVersionToolTip());
- Logger.Info("Establishing login salt");
- // allocate salt if we havn't already
- if (Session["LoginSalt"] == null)
- {
- Session["LoginSalt"] = Guid.NewGuid().ToString();
- }
- salt = (string)Session["LoginSalt"];
- Logger.Info("Login salt = " + salt);
- Logger.Info("Registering hidden field...LoginButton.UniqueID = " + LoginButton.UniqueID);
- Logger.Info(" ");
- ClientScript.RegisterHiddenField("__EVENTTARGET", LoginButton.UniqueID);
-
- }
-
- #region Web Form Designer generated code
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: This call is required by the ASP.NET Web Form Designer.
- //
- InitializeComponent();
- base.OnInit(e);
- }
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.ID = "LoginButton";
-
- }
- #endregion
-
- protected void LoginButton_Click(object sender, EventArgs e)
- {
- Logger.Info("Loginbutton click detected...");
- // check credentials
-
-
- // generate the expected hash for admin user and guest user
- string expectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredPasswordHash.ToLower() + salt, "MD5");
- string GuestexpectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredGuestPasswordHash.ToLower() + salt, "MD5");
-
-
- // direct user to "manage listings" if credentials are correct
- invalidCredentialsLabel.Visible = true;
- Logger.Info("Now pulling User Host Info...");
- if (Request.UserHostAddress.Substring(0, 1) == ":")
- {
- Logger.Info("IP V6 Detected on Server ");
- hostAddress = GetIP4Address();
- Logger.Info("Using IP4 Address: " + hostAddress);
- }
- else
- {
- hostAddress = Request.UserHostAddress;
- Logger.Info("Using IP4 Address: " + hostAddress);
- }
-
- Logger.Info(" ");
- Logger.Info("Loading Global Settings.... ");
- Logger.Info(" ");
- guideParams = Global.Settings;
- Logger.Info(" ");
- Session["NotGuestUser"] = "true";
- Logger.Info("Checking to see if the username and password that were entered match what was pulled from config....");
- if ((Username.Value == requiredUsername) &&
- (PasswordHash.Value == expectedHash.ToLower()))
- {
- Logger.Info("User name and password matched for Administrative access...");
- Logger.Info(" ");
- CompleteLogin();
- }
- else
- {
- // Create a StringComparer an comare the hashes.
- StringComparer comparer = StringComparer.OrdinalIgnoreCase;
-
- if ((0 == comparer.Compare(PasswordHash.Value, GuestexpectedHash)) && Username.Value == guideParams.GuestUser)
- {
- Session["NotGuestUser"] = "false";
- Logger.Info("User name and password matched for Guest access...");
- Logger.Info(" ");
- CompleteLogin();
- }
- else
- {
- Logger.Warning("Failed login for " + Username.Value + " from " + hostAddress);
- Logger.Info("Pasword hash value = " + PasswordHash.Value);
- Logger.Info("Expected Admin hash value = " + expectedHash.ToLower());
- Logger.Info("Expected Guest hash value = " + guideParams.GuestPassword);
- }
- }
- }
-
- private void CompleteLogin()
- {
- Logger.Info("You have made it to the complete login method....this is a good thing.... ");
-
- string userName = Username.Value;
- bool createPersistentCookie = false;
- string cookiePath = FormsAuthentication.FormsCookiePath;
- string redirectUrl = FormsAuthentication.GetRedirectUrl(userName, createPersistentCookie);
- Uri redirectUri = new Uri(Request.Url, redirectUrl);
-
- Logger.Info("RedirectUri = " + redirectUri.LocalPath);
-
- Logger.Info("Checking username not blank, redirect uri = admin/admin2.aspx... ");
- if ((Username.Value.Length == 0) &&
- (((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin.aspx", true) == 0)) ||
- ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0))))
- {
- Logger.Info("User name was blank and the redirect uri and request path were set to admin.aspx... ");
- Logger.Info(" ");
- return;
- }
-
- if (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0)
- {
- Logger.Info("User is going to css based admin2.aspx...setting theme session variable ");
- Logger.Info(" ");
- getTheme();
- }
-
- Logger.Info("Checking to see if the redirec uri is equal to default.aspx or logout.aspx ");
- if ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default.aspx", true) == 0) ||
- (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout.aspx", true) == 0))
- {
- Logger.Info(
- "Redirect uri was equal to default.aspx or logout.aspx....seting the redirect to guide.aspx...... ");
- Logger.Info(" ");
- redirectUrl = Request.ApplicationPath + "/Guide.aspx";
-
- }
- else
- {
- if ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default2.aspx", true) == 0) ||
- (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout2.aspx", true) == 0))
- {
- Logger.Info(
- "Redirect uri was equal to default2.aspx or logout2.aspx....seting the redirect to guide2.aspx...... ");
- Logger.Info(" ");
- getTheme();
- redirectUrl = Request.ApplicationPath + "/Guide2.aspx";
- }
- else
- {
- Logger.Info("Redirect uri was not equal to default or logout.... ");
- Logger.Info(" ");
- }
- }
-
- Logger.Info(" ");
- if (Username.Value.Length == 0)
- {
- Logger.Info("Automatic login from " + hostAddress);
- Session["NotGuestUser"] = "true";
- Logger.Info(" ");
-
- }
- else
- {
- Logger.Info("Successful login for " + Username.Value + " from " + hostAddress);
- Logger.Info(" ");
- }
-
- Logger.Info("Initializing Forms Authentication.... ");
- Logger.Info(" ");
- FormsAuthentication.Initialize();
- Logger.Info("Setting Forms Authentication Cookie.... ");
- Logger.Info("Username = " + Username.Value);
- Logger.Info("Cookie Path = " + cookiePath);
- Logger.Info(" ");
- FormsAuthentication.SetAuthCookie(Username.Value, createPersistentCookie, cookiePath);
-
- Logger.Info("Checking for auto search execution.... ");
- if ((guideParams.autoShowSearch || guideParams.autoShowRecord) && (String.Compare(guideParams.lastAutoSearchDate, DateTime.Now.ToShortDateString()) != 0))
- {
- Logger.Info("Auto Search found and has not yet been executed today.... ");
- XmlNode EPGHour = Global.Config.SelectSingleNode("/settings/UpdateEPGHour");
- Logger.Info("Checking if it is at least one hour past the EPG update for today.... ");
- if (DateTime.Now.Hour + 1 > Convert.ToInt32(EPGHour.InnerText))
- {
- Logger.Info("It is greater than one hour since todays EPG update.... ");
- Logger.Info("Executing auto search.... ");
- SavedSearchUtils searchUtil = new SavedSearchUtils();
- searchUtil.autoSearch(searchUtil.loadAutoSearches());
- Logger.Info("Setting last auto search date to today so this routine does not run again until tommorow.... ");
- guideParams.lastAutoSearchDate = DateTime.Now.ToShortDateString();
- guideParams.Save();
- }
- else
- {
- Logger.Info("Auto search can not execute now since it is not 1 hour greater than todays EPG update.... ");
- }
- }
- else
- {
- Logger.Info("Auto Search is not set to run or it has aleady run today.... ");
- }
- Logger.Info(" ");
- Logger.Info("Redirecting to the target page.... ");
- Logger.Info("Redirect URL = " + redirectUrl);
- Response.Redirect(redirectUrl, true);
- Logger.Info(" ");
- }
-
- public string GetIP4Address()
- {
- string strIP4Address = String.Empty;
-
- foreach (IPAddress objIP in Dns.GetHostAddresses(Dns.GetHostName()))
- {
- if (objIP.AddressFamily.ToString() == "InterNetwork")
- {
- strIP4Address = objIP.ToString();
- break;
- }
- }
- return strIP4Address;
- }
-
- private void getTheme()
- {
- //Check to see if the theme has been set in session and that the set theme is using the table view
- string theme = Convert.ToString(Session["theme"]);
-
- if (theme != null && theme != "" && theme.Substring(0, 7) == "themes/")
- {
- return;
- }
- else
- {
- //Since the session theme variable was not set or is using the css view we need to read the value from the cookie
- HttpCookie cookie = Request.Cookies["theme"];
- if (cookie != null && cookie.Value.Length > 0)
- {
- theme = cookie.Value;
- }
- else
- {
- theme = "Default";
- }
-
- //Verify that that the theme in the cookie is available in the table based selections. If it is not found set the theme to Default.
- if (File.Exists(HttpContext.Current.Server.MapPath("~/themes/") + theme + "/styles.css"))
- {
- Session["theme"] = "themes/" + theme;
- }
- else
- {
- Session["theme"] = "themes/Default";
- }
-
-
-
- return;
- }
- }
- }
- }
-